public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    //穷举的方式 创建一颗二叉树出来
    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;

    }

    //前序遍历
    public void preOrder(TreeNode root) {
        if(root == null) {
            return;//空树就不进行遍历
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }

    //中序遍历
    public void inOrder(TreeNode root) {
        if(root == null) {
            return;//空树就不进行遍历
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    //后序遍历
    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");

    }
    //获取树中结点的个数
    public static int Size;
    public void Size(TreeNode root) {
        if (root == null) {
            return;
        }
        Size(root.left);
        Size(root.right);
        Size++;
    }
    public int Size2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return Size2(root.left) +Size2(root.right) + 1;
    }
    //获取叶子结点的个数
    public static int leftSize;
    //方法一：普通方法：
    public void getLeafNodeCount(TreeNode root) {
        if (root == null) {
            return;
        }
        if(root.left == null && root.right ==null) {
            leftSize++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
    }
    //方法二：子问题
    public int getLeafNodeCount2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if(root.left == null && root.right ==null) {
            return 1;
        }
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }
    //
    public int getKLevelNodeCount(TreeNode root,int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1) +getKLevelNodeCount(root.right,k-1);
    }
    public int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return Math.max(getHeight(root.left),getHeight(root.right)) + 1;
    }

    //判断value值的元素是否存在
    public TreeNode find(TreeNode root,int val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode leftVal = find(root.left,val);
        if(leftVal != null) {
            return leftVal;
        }
        TreeNode rightVal = find(root.right,val);
        if(rightVal != null) {
            return rightVal;
        }
        return null;
    }
}
